package indi.jp.project.core.controller;

import indi.jp.project.core.entity.JsonResult;
import indi.jp.project.core.entity.Role;
import indi.jp.project.core.entity.RoleUser;
import indi.jp.project.core.service.RoleService;
import indi.jp.project.core.util.SimpleEntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.Map;

@RestController
@RequestMapping("/sys/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/queryGrid")
    @ResponseBody
    public JsonResult queryGrid(@ModelAttribute Role entity, HttpServletRequest request) {
        SimpleEntityUtil<Role> simpleEntityUtil = new SimpleEntityUtil<>();
        entity = simpleEntityUtil.setBaseEntity(entity, request);
        try {
            Map<String, Object> map = roleService.queryGrid(entity);
            return JsonResult.success(map);
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @PostMapping("/save")
    @ResponseBody
    public JsonResult save(@RequestBody Role entity, HttpServletRequest request) {
        SimpleEntityUtil<Role> simpleEntityUtil = new SimpleEntityUtil<>();
        entity = simpleEntityUtil.setBaseEntity(entity, request);
        String roleCode = entity.getCode().toUpperCase();
        entity.setCode(roleCode);
        try {
            return roleService.save(entity);
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @PostMapping("/deleteById")
    @ResponseBody
    public JsonResult deleteById(HttpServletRequest request){
        String id = request.getParameter("id");
        try {
            int effectRow = roleService.deleteById(id);
            if(effectRow > 0){
                return JsonResult.success("");
            } else {
                return JsonResult.failure("未能成功删除，请刷新查看数据是否存在");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @GetMapping("/queryRoleUserGrid")
    @ResponseBody
    public JsonResult queryRoleUserGrid(@ModelAttribute RoleUser entity, HttpServletRequest request){
        SimpleEntityUtil<RoleUser> simpleEntityUtil = new SimpleEntityUtil<>();
        entity = simpleEntityUtil.setBaseEntity(entity, request);
        try {
            Map<String, Object> map = roleService.queryRoleUserGrid(entity);
            return JsonResult.success(map);
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @PostMapping("/addRoleUser")
    @ResponseBody
    public JsonResult addRoleUser(@RequestBody RoleUser entity, HttpServletRequest request){
        return roleService.addRoleUser(entity);
    }

    @PostMapping("/removeRoleUser")
    @ResponseBody
    public JsonResult removeRoleUser(HttpServletRequest request){
        String id = request.getParameter("id");
        return roleService.removeRoleUser(id);
    }
}
